package com.lishide.scrollrecyclerview;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

public class RoundRelativeLayout extends RelativeLayout {

    private ColorStateList mSolidColor;
    private int mCornerRadius;
    public RoundRelativeLayout(Context context) {
        super(context);
    }

    public RoundRelativeLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public RoundRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public RoundRelativeLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundButton);
        float pressedRatio = a.getFloat(R.styleable.RoundButton_btnPressedRatio, 0.80f);
        mCornerRadius = a.getLayoutDimension(R.styleable.RoundButton_btnCornerRadius, getResources().getDimensionPixelSize(R.dimen.radius_default));
        mSolidColor = a.getColorStateList(R.styleable.RoundButton_btnSolidColor);
        int strokeColor = a.getColor(R.styleable.RoundButton_btnStrokeColor, Color.GRAY);
        int strokeWidth = a.getDimensionPixelSize(R.styleable.RoundButton_btnStrokeWidth, 0);
        int strokeDashWidth = a.getDimensionPixelSize(R.styleable.RoundButton_btnStrokeDashWidth, 0);
        int strokeDashGap = a.getDimensionPixelSize(R.styleable.RoundButton_btnStrokeDashGap, 0);
        a.recycle();
        RoundDrawable rd = new RoundDrawable(mCornerRadius == -1);
        rd.setCornerRadius(mCornerRadius == -1 ? 0 : mCornerRadius);
        rd.setStroke(strokeWidth, strokeColor, strokeDashWidth, strokeDashGap);

        if (mSolidColor == null) {
            mSolidColor = ColorStateList.valueOf(Color.GRAY);
        }
        if (mSolidColor.isStateful()) {
            rd.setSolidColors(mSolidColor);
        } else if (pressedRatio > 0.0001f) {
            rd.setSolidColors(csl(mSolidColor.getDefaultColor(), pressedRatio));
        } else {
            rd.setColor(mSolidColor.getDefaultColor());
        }
        setBackground(rd);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void setSolidColor(int color) {
        RoundDrawable rd = new RoundDrawable(mCornerRadius == -1);
        rd.setCornerRadius(mCornerRadius == -1 ? 0 : mCornerRadius);
        mSolidColor = ColorStateList.valueOf(color);
        if (mSolidColor.isStateful()) {
            rd.setSolidColors(mSolidColor);
        }  else {
            rd.setColor(mSolidColor.getDefaultColor());
        }
        setBackground(rd);
    }

    int darker(int color, float ratio) {
        color = (color >> 24) == 0 ? 0x22808080 : color;
        float[] hsv = new float[3];
        Color.colorToHSV(color, hsv);
        hsv[2] *= ratio;
        return Color.HSVToColor(color >> 24, hsv);
    }

    ColorStateList csl(int normal, float ratio) {
        //        int disabled = greyer(normal);
        int pressed = darker(normal, ratio);
        int[][] states = new int[][]{{android.R.attr.state_pressed}, {}};
        int[] colors = new int[]{pressed, normal};
        return new ColorStateList(states, colors);

    }

}
